package gov.pbc.nn.kjgl.software.dao;

import gov.pbc.nn.core.dao.AbstractHibernateDao;
import gov.pbc.nn.kjgl.entity.ProjectChange;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;

@Repository
public class ProjectChangeReTJDao extends AbstractHibernateDao {

	/**
	 * 获取业务干系科长审批时的相关数据
	 * 
	 * @param id
	 * @return
	 */
	public Map<String, Object> loadProjectByChange(String id) {
		final String sql = "select a.ID_ as pid,a.NAME_,a.DEPT_NAME_,b.ID_ as pcid,b.CHANGE_NAME_,b.CHANGE_DESCRIBE_,b.STATE_ from t_project as a inner join t_project_change as b on a.ID_ = b.PROJECT_ID_ where b.ID_= '"
				+ id + "' and b.INVALID_=0";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", id);

		List<Object[]> ls = this
				.doInHibernateSession(new ISessionCallback<List<Object[]>>() {

					@Override
					public List<Object[]> doInSession(Session session) {
						return session.createSQLQuery(sql).list();
					}

				});
		Map<String, Object> map = new HashMap<String, Object>();
		if (!ls.isEmpty()) {
			for (Object[] list : ls) {
				map.put("projectId", list[0]);
				map.put("projectName", list[1]);
				map.put("deptName", list[2]);
				map.put("changeId", list[3]);
				map.put("changeName", list[4]);
				map.put("changeDescribe", list[5]);
				map.put("state", list[6]);
				return map;
			}
		}
		return null;
	}

	/**
	 * 根据id 获取 这个对象
	 * 
	 * @param id
	 * @return
	 */
	public ProjectChange getProjectChange(String id) {
		String hql = "from ProjectChange where invalid=false and id=:id ";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		List<ProjectChange> ls = this.query(hql, map);
		if (ls.size() > 0) {
			return ls.get(0);
		} else {
			return null;
		}
	}
}
